package com.xizi.forum.controller;

import com.xizi.forum.common.AppResult;
import com.xizi.forum.common.ResultCode;
import com.xizi.forum.exception.ApplicationException;
import com.xizi.forum.model.BoardInfo;
import com.xizi.forum.service.IBoardInfoService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 板块模块
@RestController
@RequestMapping("/board")
public class BoardInfoController {

    @Autowired
    private IBoardInfoService boardInfoService;

    /**
     * 获取版块链表
     * @return
     */
    @RequestMapping("/topList")
    public AppResult<List<BoardInfo>> topList(){
        AppResult<List<BoardInfo>> result = boardInfoService.selectListNum();
        return result;
    }

    /**
     * 获取其中一个版块
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById",method = RequestMethod.GET,produces = "application/json")
    public AppResult<BoardInfo> getById(@NonNull Long id) throws Exception {
        BoardInfo boardInfo = boardInfoService.getBoardInfoById(id);

        if(boardInfo == null || boardInfo.getDeleteState() == 1){
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS);
        }

        return AppResult.success(boardInfo);
    }

}
